package Servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

import Service.ProjectService;

import pojo.PageBean;
import pojo.Project;
import pojo.User;



/**
 * Servlet implementation class ProjectServlet
 */
@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String img;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		System.out.println("1111"+method);
		if(method.equals("del")) {
			del(request,response);
		}else if(method.equals("add")) {
			add(request,response);
		}else if(method.contentEquals("showAll")) {
			showAll(request,response);
		}else if(method.equals("update")) {
			update(request,response);
		}else if(method.equals("up")) {
			up(request,response);
		}else if(method.equals("showone")){
			showone(request,response);
		}
	}
		private void showAll(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
	    	int currentPage = Integer.parseInt(request.getParameter("currentPage"));
	    	int pageSize=5;
	    	ProjectService ps = new ProjectService();
	    	PageBean<Project> pb = ps.show(currentPage, pageSize,"");
//	    	HttpSession session=request.getSession(true);
//			session.setAttribute("pb", pb);
	    	request.setAttribute("pb", pb);
	    	request.getRequestDispatcher("show/3.jsp").forward(request, response);
//	    	try {
//				try {
//					request.getRequestDispatcher("show/3.jsp").forward(request, response);
//				} catch (ServletException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
	    }
		private void showone(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
	    	int currentPage = Integer.parseInt(request.getParameter("currentPage"));
	    	int pageSize=5;
	    	HttpSession session=request.getSession(true);
	    	User t = (User)(session.getAttribute("c"));
	    	ProjectService ps = new ProjectService();
	    	PageBean<Project> pb = ps.show(currentPage, pageSize, t.getUsername());
	    	request.setAttribute("pb", pb);
	    	request.getRequestDispatcher("show/3.jsp").forward(request, response);
		}
		private void add(HttpServletRequest request, HttpServletResponse response) {
			// TODO Auto-generated method stub
			try {
				request.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
//			SmartUpload su = new SmartUpload();
//			PageContext context = JspFactory.getDefaultFactory()
//					.getPageContext(this, request, response, null, true, 8*1024, true);
//			try {
//				su.initialize(context);
//				su.setMaxFileSize(2*1024*1024);
//				su.setAllowedFilesList("jpg,gif,png");	
//				su.upload();
//				com.jspsmart.upload.File file1 = su.getFiles().getFile(0); 
//				String filename = UUID.randomUUID()+"."+file1.getFileExt();
//				System.out.println("servlet....."+filename);
//				String path = request.getServletContext().getRealPath("/images");
//				java.io.File f = new java.io.File(path);
//				if(!f.exists()) {
//					f.mkdir();
//				}
//				file1.saveAs(request.getServletContext().getRealPath("/images/"+filename));
//				Request rq  = su.getRequest();
			Project p =new Project();
			p.setPname(request.getParameter("pname"));
			p.setUnit(request.getParameter("unit"));
			p.setPrincipal(request.getParameter("principal"));
			p.setState(request.getParameter("state"));
			p.setPdate(request.getParameter("pdate"));
			p.setExpenditure(request.getParameter("expenditure"));
			p.setDirection(request.getParameter("direction"));
			p.setRate(request.getParameter("rate"));
//			try {
//				Part part = request.getPart("image");
//				String filename=part.getSubmittedFileName();//
//				
//				String path=this.getServletContext().getRealPath("image");
//				File f = new File(path);
//				if(!f.exists()) {
//					f.mkdir();
//				}
//				String  name=UUID.randomUUID()+filename;
//				img="image/"+name;
//				part.write(path+"/"+name);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (ServletException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			p.setImage(img);
			ProjectService pservice = new ProjectService();
			int i = pservice.add(p);
			if(i>0) {
				try {
					response.sendRedirect("ProjectServlet?method=showAll&currentPage=1");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				try {
					response.sendRedirect("add/xiangmu.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
			private void up(HttpServletRequest request, HttpServletResponse response) {
				// TODO Auto-generated method stub
				String id=request.getParameter("id");
				ProjectService pservice = new ProjectService();
				Project p = pservice.getProject(id);
				if(p!=null) {
					HttpSession session=request.getSession(true);
					session.setAttribute("P", p);
					try {
						response.sendRedirect("update/UpdateProject.jsp");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			private void update(HttpServletRequest request, HttpServletResponse response) {
				// TODO Auto-generated method stub
				try {
					request.setCharacterEncoding("utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				String id=request.getParameter("id");
				String pname=request.getParameter("pname");
				String unit=request.getParameter("unit");
				String principal=request.getParameter("principal");
				String state = request.getParameter("state");
				String pdate=request.getParameter("pdate");
				String expenditure=request.getParameter("expenditure");
				String direction=request.getParameter("direction");
				String rate = request.getParameter("rate");
//				Part part;
//				try {
//					part = request.getPart("image");
//					String filename=part.getSubmittedFileName();
//					String str = this.getServletContext().getRealPath("image");
//					File f=new File(str);
//					if (!f.exists()){
//						f.mkdir();
//					}
//				    String name1 = UUID.randomUUID()+filename;
//				    img="images/"+name1;
//					part.write(str+"/"+name1);
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//					
//				} catch (ServletException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				
				Project p =new Project();
				p.setId(id);
				p.setPname(pname); 
				p.setUnit(unit);
				p.setPrincipal(principal);
				p.setState(state);
				p.setPdate(pdate);
				p.setExpenditure(expenditure);
				p.setDirection(direction);
				p.setRate(rate);
				ProjectService pservice=new ProjectService();
				int i = pservice.update(p);
				if(i>0) {
					try {
						response.sendRedirect("http://localhost:8080/kygl/ProjectServlet?method=showAll&currentPage=1");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					try {
						response.sendRedirect("update/UpdateProject.jsp");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		}
			private void del(HttpServletRequest request, HttpServletResponse response) {
				// TODO Auto-generated method stub
				String id=request.getParameter("id");
				ProjectService cservice = new ProjectService();
				int flag = cservice.delProject(id);
				if(flag>0){
				try {
					response.sendRedirect("ProjectServlet?method=showAll&currentPage=1");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}else {
					try {
						response.sendRedirect("fail.jsp");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}